﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TheCell;

namespace SudoCalc
{
    class CheckUtils
    {
        public static bool Check(List<Cell> cells)
        {
            Dictionary<int, List<Cell>> lines = GetPart(cells, 0);
            Dictionary<int, List<Cell>> cols = GetPart(cells, 1);
            Dictionary<int, List<Cell>> areas = GetPart(cells, 2);
            bool result = Check(lines);
            if (result)
            {
                result = Check(cols);
                if (result)
                {
                    result = Check(areas);
                }
            }

            return result;
        }

        private static bool Check(Dictionary<int, List<Cell>> pairs)
        {
            bool result = true;
            foreach (int key in pairs.Keys)
            {
                result = PartialCheck(pairs[key]);
                if (!result)
                {
                    break;
                }
            }

            return result;
        }

        private static Dictionary<int, List<Cell>> GetPart(List<Cell> cells, int type)
        {
            Dictionary<int, List<Cell>> result = new Dictionary<int, List<Cell>>();
            for (int i = 1; i < 10; i++)
            {
                List<Cell> list = null;
                if (result.Keys.Contains(i))//容错，也许会用到
                {
                    list = result[i];
                }
                else
                {
                    list = new List<Cell>();
                    result.Add(i, list);
                }

                foreach (Cell cell in cells)
                {
                    Cell target = null;
                    switch (type)
                    {
                        case 0:
                            if (cell.Location.X == i)
                            {
                                target = cell;
                            }
                            break;
                        case 1:
                            if (cell.Location.Y == i)
                            {
                                target = cell;
                            }
                            break;
                        case 2:
                            if (cell.Location.Area == i)
                            {
                                target = cell;
                            }
                            break;
                    }

                    list.Add(target);
                }
            }

            return result;
        }
        
        private static bool PartialCheck(List<Cell> cells)
        {
            bool result = true;
            List<int> list = new List<int>();
            foreach (Cell part in cells )
            {
                if (list.Contains(part.Value))
                {
                    result = false;
                    break;
                }
                else
                {
                    list.Add(part.Value);
                }
            }
            return result;
        }
    }
}


/*Plan one ×
 *        for (int row = 1; row < 10; row++)
            {
                if ()
                {
                    return false;
                }
            }
          for (int col = 1; col < 10; col++)
            {
                if ()
                {
                    return false;
                }
            }
          for (int Area = 1; Area < 10; Area++)
            {
                if ()
                {
                    return false;
                }
            }
            return true;*/

/*Plan two ×
    for (int i = 1; i < 10; i++)
{
    for(int j = 1; j < 10; j++)
    {
        if (i != j && i.value = j.value)
        {
            return false
        }
    }
}
return true;
 */

/*Plan three ×
 i
 👇
|  |  |  |  |  |  |  |  |  |
    👆
    j
  for (i = 0; i < count - 1; i++)
    {
        for (j = i + 1; j < count; j++)
        {

        }
    }
 */


